<?php

namespace app\live\controller;

use app\common\model\GoodsModel;
use app\common\model\live\AskModel;
use app\common\model\live\GiftModel;
use app\common\model\live\MallModel;
use app\common\model\live\OnlineModel;
use app\common\model\live\PayModel;
use app\common\model\live\RoomModel;
use app\common\model\live\RoomNoticeModel;
use app\common\model\live\RoomTabModel;
use app\common\model\MemberModel;
use think\facade\Cookie;
use wechat\Factory;

class MemberController extends BaseController
{

  private $roomModel;
  private $roomLogin;
  private $wechat;
  private $memberModel;


  /**
   * 初始化
   * @throws \Exception
   */
  public function __construct()
  {
    parent::__construct();
    if ($this->checkMemberLogin() === false) $this->redirect('/live/play/login', ['redirect' => urlencode($this->request->url(true))]);
    $this->roomModel   = new RoomModel();
    $this->mallModel = new MallModel();
    $this->goodsModel = new \app\common\model\live\GoodsModel();
    $this->wechat      = Factory::officialAccount();
    $this->memberModel = new MemberModel();
    $this->roomLogin   = $this->getMemberLogin();
    $this->payModel = new PayModel();
    $wxJsApi = $this->wechat->jssdk->buildConfig(['editAddress']);
    $this->assign('wx_js_api', $wxJsApi);
    $this->assign('room_login', $this->roomLogin);
  }


  /**
   * 会员首页
   * @return string
   * @throws \Exception
   */
  public function index()
  {
    return $this->fetch();
  }

  /**
   * 购买商品
   * @return string
   * @throws \Exception
   */
  public function buy(){
    $goodsId = $this->request->param('goods_id');
    $goodsInfo = $this->goodsModel->where('id',$goodsId)->find();
    if(empty($goodsInfo)) abort(500,'商品信息不存在');
    if($goodsInfo['is_sale']==-1) abort(500,'商品已下架');
    if(!empty($goodsInfo['goods_link'])){
      $this->redirect($goodsInfo['goods_link']);
    }
    //默认地址
    $addressInfo = $this->payModel->where('from_user_id',$this->roomLogin['userId'])->order('id desc')->value('address_info');
    $roomInfo  = $this->roomModel->where('room_id', $goodsInfo['room_id'])->find();
    $playUrl             = $this->roomModel->getPlayUrl($goodsInfo['room_id'], $roomInfo['host_name']);
    $this->assign('room_info',$roomInfo);
    $this->assign('goods_info',$goodsInfo);
    $this->assign('address_info',$addressInfo);
    $this->assign('play_url',$playUrl);
    return $this->fetch();
  }

  /*会员订单*/
  public function orderList()
  {
    $roomId = $this->request->param('room_id');
    if($this->request->isAjax()){
      $query = [];
      if(!empty($roomId)) $query[] = ['room_id','=',$roomId];
      $query[] = ['from_user_id','=',$this->roomLogin['userId']];
      $query[] = ['pay_type','=',3];
      $query[] = ['is_fail','=',0];
      $type = $this->request->param('order_type',1);
      switch ($type){
        case 1:
          $query[] = ['is_pay','=',0];
          break;
        case 2:
          $query[] = ['is_pay','=',1];
          $query[] = ['is_delivery','=',0];
          break;
        case 3:
          $query[] = ['is_pay','=',1];
          $query[] = ['is_delivery','=',1];
          break;
        case 4:
          $query[] = ['is_pay','=',1];
          $query[] = ['is_delivery','=',2];
          break;
      }
      $data = $this->getPage('live.Pay',[],[],$query);
      $data['rows'] = empty($data['rows'])?[]:$this->payModel->_formatList($data['rows']);
      return $data;
    }
    $roomInfo = RoomModel::where('room_id',$roomId)->find();
    $this->assign('room_info',$roomInfo);
    return $this->fetch();
  }

  /*订单详情*/
  public function orderInfo(){
    $orderSn = $this->request->param('order_sn');
    $orderInfo = $this->payModel->where('order_sn',$orderSn)->find();
    $orderInfo = $orderInfo->isEmpty()?[]:$this->payModel->_formatInfo($orderInfo);
    $this->assign('info',$orderInfo);
    return $this->fetch();
  }

  /*确认收货*/
  public function confirmOrder(){
    $orderSn = $this->request->param('order_sn');
    $this->payModel->where('from_user_id',$this->roomLogin['userId'])->where('order_sn',$orderSn)->update(['is_delivery'=>2,'delivery_time'=>time()]);
    $this->success('确认收货');
  }


}